package com.tuacy.designwidget.module.behavior.fab;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;

public class FabBottomInOutBehavior extends FloatingActionButton.Behavior {

	private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();

	private boolean mAnimatingOut = false;

	public FabBottomInOutBehavior() {
	}

	public FabBottomInOutBehavior(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
									   @NonNull FloatingActionButton child,
									   @NonNull View directTargetChild,
									   @NonNull View target,
									   int axes,
									   int type) {
		//需要垂直的滑动
		return axes == ViewCompat.SCROLL_AXIS_VERTICAL ||
			   super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);
	}


	@Override
	public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
							   @NonNull FloatingActionButton child,
							   @NonNull View target,
							   int dxConsumed,
							   int dyConsumed,
							   int dxUnconsumed,
							   int dyUnconsumed,
							   int type) {
		super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
		if (dyConsumed > 0 && !mAnimatingOut) {
			//向上滑动
			animateOut(child);
		} else if (dyConsumed < 0) {
			//向下滑动
			animateIn(child);
		}
	}

	private void animateOut(final FloatingActionButton button) {
		ViewCompat.animate(button)
				  .translationY(button.getHeight() + getMarginBottom(button))
				  .setInterpolator(INTERPOLATOR)
				  .withLayer()
				  .setListener(new ViewPropertyAnimatorListener() {
					  public void onAnimationStart(View view) {
						  mAnimatingOut = true;
					  }

					  public void onAnimationCancel(View view) {
						  mAnimatingOut = false;
					  }

					  public void onAnimationEnd(View view) {
						  mAnimatingOut = false;
					  }
				  })
				  .start();
	}


	private void animateIn(FloatingActionButton button) {
		ViewCompat.animate(button).translationY(0).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();
	}

	private int getMarginBottom(View v) {
		final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
		if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
			return ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
		}
		return 0;
	}
}
